home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / System / Capture ƒ / CaptureEdit.c < prev   
Text File  |  1990-02-07  |  8KB  |  311 lines

  1. /* Capture program - example of serial port control
  2. *
  3. *  Programmer:        James R. Logan Jr.
  4. *  Organization:    Brigham Young University
  5. */
  6.  
  7. #include "QuickDraw.h"
  8. #include "MacTypes.h"
  9. #include "WindowMgr.h"
  10. #include "TextEdit.h"
  11. #include "ControlMgr.h"
  12. #include "EventMgr.h"
  13.  
  14. #include "capture.h"
  15.  
  16. TEHandle mainTEHandle;
  17.  
  18. int        linesInFolder;
  19.  
  20.  
  21. int
  22. GetLinesInFolder( tH)
  23. TEHandle    tH;
  24. {
  25. int            local_linesInFolder, wsize;
  26. int            height;
  27. FontInfo    myInfo;
  28.  
  29.     GetFontInfo( &myInfo );
  30.     height = myInfo.ascent+myInfo.descent+myInfo.leading;
  31.     local_linesInFolder = 
  32.         (wsize=(( **tH).viewRect.bottom - ( **tH).viewRect.top)) / height;
  33.     return( local_linesInFolder);
  34. }
  35.  
  36.  
  37. /* Make the line in the TextEdit record with with the 
  38.    "insertion point" visible */
  39.  
  40. ShowSelect(mywindow,mycontrol,mytexthandle) 
  41. WindowPtr mywindow; ControlHandle mycontrol; TEHandle mytexthandle; {
  42. register int    theLine, point;
  43. int                curLine, adjust;
  44. GrafPtr savePort;
  45.   GetPort( &savePort );
  46.   SetPort( mywindow );
  47.   linesInFolder = GetLinesInFolder( mytexthandle);
  48.  
  49.   SetVScrollMax( mycontrol, mytexthandle);
  50.   curLine = GetCtlValue( mycontrol );
  51.     
  52.   point = (**mytexthandle).teLength-1;    /* (**mytexthandle).selStart;    */
  53.   for (theLine=0;                       /* Find the line where the control points */
  54.     ((point<(**mytexthandle).lineStarts[theLine]) || 
  55.     (point>=(**mytexthandle).lineStarts[theLine+1]))
  56.     ; theLine++ ) ;
  57.  
  58.   if ( (theLine<curLine ) || (theLine>(curLine+linesInFolder-1))) {
  59.     adjust = (linesInFolder-1)/2;
  60.     SetCtlValue( mycontrol, theLine - adjust );
  61.   }
  62.  
  63.   if(theLine!=curLine) ScrollBits(mycontrol, mytexthandle);
  64.  
  65.   SetPort( savePort );
  66. }
  67.  
  68.  
  69. /* The following routines scroll the TextEdit record within the 
  70.    display window */
  71.  
  72. ScrollMax(myControl,myTEHandle)
  73. ControlHandle myControl; TEHandle myTEHandle; {
  74.     SetVScrollMax( myControl, myTEHandle);
  75. /* NOTE: the topline of the TE is saved in WRefCon    */
  76.       SetWRefCon((*myTEHandle)->inPort,0L);    
  77.       SetCtlValue(    myControl,0);        /* ???    */
  78. }
  79.  
  80.  
  81.     /* TBD rename    */
  82. Scroll0(myControl,myTEHandle) 
  83. ControlHandle myControl; TEHandle myTEHandle; 
  84. {
  85. /* NOTE: the topline of the TE is saved in WRefCon    */
  86.       SetWRefCon((*myTEHandle)->inPort,0L);    
  87.     SetCtlMax(         myControl, 0);
  88.       SetCtlValue(    myControl,0);        /* ???    */
  89. }
  90.  
  91.  
  92. ScrollBits(        myControl, myTEHandle) 
  93. ControlHandle     myControl; TEHandle myTEHandle; 
  94. {
  95. long     oldtop = GetWRefCon((*myTEHandle)->inPort);
  96. long    topline= GetCtlValue(myControl);
  97. int linevalue;
  98.  
  99. /* The only place WRefCon is changed, other than when it is set to 0    */
  100.   SetWRefCon((*myTEHandle)->inPort,topline);    
  101.   linevalue = oldtop - topline;
  102.   TEScroll(0, linevalue * (*myTEHandle)->lineHeight, myTEHandle);    /*     */
  103. }
  104.  
  105.  
  106. pascal scrollup(myControl,partcode) 
  107. ControlHandle myControl; int partcode; {
  108. int value;
  109.   if (partcode == inUpButton) {
  110.     value = GetCtlValue(myControl) - 1;
  111.     SetCtlValue(myControl,value);
  112.     ScrollBits(myControl,mainTEHandle);
  113.   }
  114. }
  115.  
  116.  
  117. pascal scrolldown(myControl,partcode) 
  118. ControlHandle myControl; int partcode; {
  119. int value;
  120.   if (partcode == inDownButton) {
  121.     value = GetCtlValue(myControl) + 1;
  122.     SetCtlValue(myControl,value);
  123.     ScrollBits(myControl,mainTEHandle);
  124.   } 
  125. }
  126.  
  127.  
  128. pagescroll(myControl,whichpart,myTEHandle) 
  129. ControlHandle myControl; int whichpart; TEHandle myTEHandle; { 
  130. Point myPoint; 
  131. int amount,value; 
  132.  
  133.   if (whichpart == inPageUp) 
  134.           amount = -1; 
  135.   else     amount = 1; 
  136.   
  137.   do { 
  138.     GetMouse(&myPoint); 
  139.     if (TestControl( myControl, myPoint) == whichpart) {
  140.       value = GetCtlValue(myControl) + amount * 
  141.         (((*myTEHandle)->destRect.bottom - (*myTEHandle)->destRect.top) >> 1) / 
  142.           (*myTEHandle)->lineHeight; 
  143.       SetCtlValue(    myControl,value); 
  144.       ScrollBits(    myControl,myTEHandle); 
  145.     } 
  146.   } while (StillDown());
  147. }
  148.  
  149. /* Process scroll bar controls */
  150.  
  151. myControls( g, myWindow, myTEHandle, myPoint) 
  152. screen *g; 
  153. WindowPtr myWindow; TEHandle myTEHandle; 
  154. Point *myPoint; {
  155. int     t,whichpart;
  156. ControlHandle myControl;
  157. GrafPtr savePort;
  158.   GetPort( &savePort );
  159.   SetPort( (*myTEHandle)->inPort );
  160.  
  161.   mainTEHandle = myTEHandle;
  162.   whichpart = FindControl( *myPoint, myWindow, &myControl);
  163.   if ((myControl == g->storybar)) {
  164.     switch (whichpart) {
  165.     case inUpButton:     t = TrackControl(myControl, *myPoint,&scrollup); break;
  166.     case inDownButton:     t = TrackControl(myControl, *myPoint,&scrolldown); break;
  167.     case inPageUp:      pagescroll(myControl,whichpart,myTEHandle); break;
  168.     case inPageDown: pagescroll(myControl,whichpart,myTEHandle); break;
  169.     case inThumb: 
  170.       t = TrackControl(    myControl, *myPoint,0L); 
  171.       ScrollBits(         myControl, myTEHandle);
  172.       break;
  173.     default: break;
  174.   } }
  175.   SetPort( savePort );
  176.  
  177.  
  178. storytext(myWindowPtr,myTEHandle,mybar,wdx) 
  179. WindowPtr myWindowPtr; TEHandle myTEHandle; ControlHandle mybar; int wdx; 
  180. {
  181.   (*myTEHandle)->viewRect.right = wdx - 15;
  182.   (*myTEHandle)->destRect.right = (*myTEHandle)->viewRect.right - 2;
  183.   (*myTEHandle)->viewRect.bottom = 
  184.         myWindowPtr->portRect.bottom - myWindowPtr->portRect.top;
  185.  
  186.   TECalText(myTEHandle);
  187.   SetVScrollMax( mybar, myTEHandle);
  188.   if (GetWRefCon((*myTEHandle)->inPort) > GetCtlValue(mybar)) 
  189.     ShowSelect(myWindowPtr,mybar,myTEHandle);
  190. }
  191.  
  192.  
  193. SetVScrollMax( mybar, myTEHandle)
  194. ControlHandle mybar;
  195. TEHandle myTEHandle; 
  196. {
  197. int ctlMax, n= (*myTEHandle)->nLines - GetLinesInFolder( myTEHandle);
  198.  
  199. /* if last line ends in a \r, one more line    */
  200.   if ( IsLastCharacterCR( myTEHandle)) 
  201.     n += 1;
  202.   SetCtlMax( mybar, ctlMax = ( n>0 ? n : 0 ) );
  203. }
  204.  
  205.  
  206. int                                /* (ODD ADDR ERR) if last line ends in a \r    */
  207. IsLastCharacterCR( myTEHandle)
  208. TEHandle myTEHandle; {
  209. char    *cptr;    
  210.     cptr = *((**myTEHandle).hText);            /* point to text    */
  211.  
  212.     cptr += (**myTEHandle).teLength;    /* point to last char of text    */
  213.     return ( ((**myTEHandle).teLength>0) && (*(cptr - 1) == '\r') ) ;
  214. }
  215.  
  216.  
  217. storygrow(myWindowPtr,mybar,mytop) 
  218. WindowPtr myWindowPtr; ControlHandle mybar; int mytop; {
  219.                 /* Adjust the position and size of the scroll bars */
  220.   HideControl(mybar);
  221.   MoveControl(mybar,    myWindowPtr->portRect.right - 15, mytop - 1);
  222.   SizeControl(mybar,16, myWindowPtr->portRect.bottom - mytop - 13);
  223.   ShowControl(mybar);
  224. }
  225.  
  226.  
  227. FixStoryWindow(g,deletetext,height,width) 
  228. screen *g; Boolean deletetext; int height,width; {
  229. Rect mybox;
  230.  
  231.   SetPort(g->storyWindow);
  232.   if (deletetext) 
  233.       DeleteAllText(  g->storyth);
  234.  
  235.   SizeWindow(    g->storyWindow,width,height,TRUE);
  236.   storytext(    g->storyWindow,g->storyth,g->storybar,width);
  237.   storygrow(    g->storyWindow,g->storybar,g->storyWindow->portRect.top);
  238.   ShowWindow(    g->storyWindow);
  239.  
  240. /* Erase the previous text - required to erase the grow box sometimes */
  241.   SetRect(    &mybox,0,0,width - 15,height);
  242.   EraseRect(&mybox);
  243.  
  244. /* Invalidate the whole Window - required to redisplay the grow box */
  245.   SetRect(    &mybox,0,0,width,height);
  246.   InvalRect(&mybox);
  247. }
  248.  
  249.  
  250. /* Clear the text window */
  251.  
  252. clearrem(g) screen *g; {
  253. GrafPtr savePort;
  254.   GetPort( &savePort );
  255.   SetPort( g->storyWindow );
  256.   ClearText( g);
  257.   SetPort( savePort );
  258. }
  259.  
  260.  
  261. ClearText(g) screen *g; {
  262.     DeleteAllText( g->storyth);
  263.       Scroll0(g->storybar,g->storyth);
  264. }
  265.  
  266.  
  267. DeleteAllText(  myTEHandle)
  268. TEHandle myTEHandle; {
  269.   TESetSelect(0L,32768L, myTEHandle); 
  270.   TEDelete( myTEHandle); 
  271.   (*myTEHandle)->destRect = (*myTEHandle)->viewRect;    /* reset destRect    */
  272. }
  273.  
  274. /* Print a C comment "..." to the txt window    */
  275.  
  276. ccomment(g,mytext)
  277. screen *g; char *mytext; {
  278. long mycount = strlen(mytext);
  279. GrafPtr savePort;
  280.   GetPort( &savePort );
  281.   SetPort( g->storyWindow );
  282.   TEInsert(mytext,mycount,g->storyth);
  283.   storytext(    g->storyWindow,g->storyth,g->storybar,g->storyWindow->portRect.right);
  284.   ScrollBits(    g->storybar,g->storyth);
  285.   SetPort( savePort );
  286. }
  287.  
  288. /* Print a Pascal comment "\p..." to the txt window    */
  289.  
  290. pcomment(g,mytext)
  291. screen *g; char *mytext; {
  292. long mycount; char mychar,*myptr;
  293. GrafPtr savePort;
  294.  
  295.   GetPort( &savePort );
  296.   SetPort( g->storyWindow );
  297.   mychar  = *mytext;
  298.   mycount = mychar & 0xff;
  299.   myptr   = mytext + 1;
  300.   
  301.   if( !mytext) return;
  302.  
  303.   if(*mytext == 0) return;
  304.  
  305.   TEInsert( myptr, mycount, g->storyth);
  306.   storytext( g->storyWindow, g->storyth, g->storybar, g->storyWindow->portRect.right);
  307.   ScrollBits( g->storybar,g->storyth);
  308.   SetPort( savePort );
  309. }
  310.